package com.juntuo.platformUtils;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.juntuo.Enums.Constants;
import com.juntuo.entity.Device;
import com.juntuo.util.HttpsUtil;
import com.juntuo.util.StreamClosedHttpResponse;
import com.juntuo.util.StringUtil;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class QueryDevicesUtil {
    public static  PageInfo<Device> queryDevices(Integer pageNo, Integer pageSize,Integer status,String startTime,String endTime) throws Exception {

        // Two-Way Authentication
        HttpsUtil httpsUtil = new HttpsUtil();
        httpsUtil.initSSLConfigForTwoWay();

        // Authentication，get token
        String accessToken = AuthenticationUtil.login(httpsUtil);

        //Please make sure that the following parameter values have been modified in the Constant file.
        String appId = Constants.APP_ID;
        String urlQueryDevices = Constants.QUERY_DEVICES;

        //please replace the pageNo and pageSize, when you use the demo.

        pageNo=pageNo-1;
        Map<String, String> paramQueryDevices = new HashMap<>();
        paramQueryDevices.put("appId", appId);
        paramQueryDevices.put("pageNo", pageNo.toString());
        paramQueryDevices.put("pageSize", pageSize.toString());
        if(status!=null){
            switch (status){
                case 0:
                    paramQueryDevices.put("status","OFFLINE");
                    break;
                case 1:
                    paramQueryDevices.put("status","ONLINE");
                    break;
                case 2:
                    paramQueryDevices.put("status","ABNORMAL");
                    break;
                    default:break;
            }
        }
        if (!StringUtil.strIsNullOrEmpty(startTime)){
            //todo
        }
        if (!StringUtil.strIsNullOrEmpty(endTime)){
            //todo
        }
        Map<String, String> header = new HashMap<>();
        header.put(Constants.HEADER_APP_KEY, appId);
        header.put(Constants.HEADER_APP_AUTH, "Bearer" + " " + accessToken);

        StreamClosedHttpResponse bodyQueryDevices = httpsUtil.doGetWithParasGetStatusLine(urlQueryDevices,
                paramQueryDevices, header);

        System.out.println("QueryDevices, response content:");
        System.out.print(bodyQueryDevices.getStatusLine());
        System.out.println(bodyQueryDevices.getContent());

        Map<String,Object> pagedResults= (Map<String, Object>) JSON.parse(bodyQueryDevices.getContent());
        List<Map<String, Object>> devicesMap = (List<Map<String, Object>>) pagedResults.get("devices");
        List<Device> devices = DeviceMap2DeviceUtil.toDeviceDatas(devicesMap);
        PageInfo pageInfo= new PageInfo(devices);
        pageInfo.setTotal(Long.parseLong(pagedResults.get("totalCount").toString()));
        pageInfo.setPageNum(pageNo+1);
        pageInfo.setPageSize(pageSize);
        return pageInfo;
    }

    public static void main(String[] args) throws Exception {
        PageInfo<Device> pageInfo=QueryDevicesUtil.queryDevices(1,20,null,null,null);
        List<Device> devices = pageInfo.getList();
        for(Device device:devices){
            device.setCreateTime(new Date());
        }

    }
}
